package com.lnlyj.media.ui;

import android.app.Application;

import com.lnlyj.media.data.AlbumItem;
import com.lnlyj.media.data.MediaGroup;
import com.lnlyj.media.data.load.LoadManager;
import com.lnlyj.media.data.load.MediaLoader;
import com.lnlyj.media.data.load.QueryConfig;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

/**
 * wanglei 2020-04-16
 */
public class MediaViewModel extends AndroidViewModel implements MediaLoader.OnMediaLoaderCallback {

    private MutableLiveData<List<MediaGroup>> mediaLiveData = new MutableLiveData<>();
    private MutableLiveData<List<AlbumItem>> albumLiveData = new MutableLiveData<>();

    public MediaViewModel(@NonNull Application application) {
        super(application);
    }

    public void getMediaData(QueryConfig config) {
        List<MediaGroup> mediaGroups = LoadManager.getInstance().loadMediaData(config, this);
        onLoaderFinish(mediaGroups);
    }

    public void getAlbumData() {
        List<AlbumItem> albumItems = LoadManager.getInstance().loadAlbumData(list -> {
            albumLiveData.postValue(list);
        });
        albumLiveData.postValue(albumItems);
    }

    public MutableLiveData<List<MediaGroup>> getMediaLiveData() {
        return mediaLiveData;
    }

    public MutableLiveData<List<AlbumItem>> getAlbumLiveData() {
        return albumLiveData;
    }

    @Override
    public void onLoaderFinish(List<MediaGroup> list) {
        mediaLiveData.postValue(list);
    }
}
